package com.suray.wcs.service.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.suray.wcs.service.enums.NetAddressType;
import com.suray.wcs.service.system.entity.NetAddressManageDB;
import com.suray.wcs.service.system.mapper.NetAddressManageDBMapper;
import com.suray.wcs.service.system.service.NetAddressManegeDBService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author jie.min
 * @since 2021-11-24
 */
@Service
public class NetAddressManageDBServiceImpl extends ServiceImpl<NetAddressManageDBMapper, NetAddressManageDB>
        implements NetAddressManegeDBService {

    @Resource
    NetAddressManageDBMapper netAddressManageDBMapper;
    private Logger log = LoggerFactory.getLogger(this.getClass());
    @Override
    public List<NetAddressManageDB> findNetAddressByType(NetAddressType type) {
        QueryWrapper<NetAddressManageDB> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(NetAddressManageDB::getType, type);

        List<NetAddressManageDB> netAddressManageDBS = list(queryWrapper);

        if (netAddressManageDBS.size() == 0) {
            return new ArrayList<>();
        }

        return netAddressManageDBS;
    }
    /**
     * 分页查询
     *
     * @param devicedb
     * @return
     * @throws Exception
     */
    @Override
    public List<NetAddressManageDB> findDeviceList(NetAddressManageDB devicedb, NetAddressType plcDeviceType) {
        QueryWrapper<NetAddressManageDB> queryWrapper = new QueryWrapper<NetAddressManageDB>();
        if (devicedb.getType() != null) {
            queryWrapper.lambda().eq(NetAddressManageDB::getType, plcDeviceType);
        }
        return netAddressManageDBMapper.selectList(queryWrapper);
    }
    /**
     * 插入数据库，
     *
     * @param netAddressManageDB
     */
    @Override
    public void insertDevice(NetAddressManageDB netAddressManageDB) {
        if (netAddressManageDB != null) {
            this.save(netAddressManageDB);
            log.info("插入设备 " + netAddressManageDB);
        }
    }

    /**
     * 更新设备
     */
    @Override
    public void updateDevice(NetAddressManageDB netAddressManageDB) {
        if (netAddressManageDB.getId() != null) {
            netAddressManageDBMapper.updateById(netAddressManageDB);
            log.info("更新设备" + netAddressManageDB);
        }
    }

    /**
     * @return List<NetAddressManageDB>
     * @Description:根据设备类型查询外部设备设备
     * @author rbj
     */
    @Override
    public List<NetAddressManageDB> querySonRgvList(NetAddressType type) {
        QueryWrapper<NetAddressManageDB> queryWrapper = new QueryWrapper<NetAddressManageDB>();
        queryWrapper.lambda().eq(NetAddressManageDB::getType, type);
        List<NetAddressManageDB> sonRvgList = this.list(queryWrapper);
        return sonRvgList;
    }
}
